//
//  SN_Question_Detail_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/22.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Question_Detail_Controller.h"

#import "SN_Question_Detail_BottomView.h"

#import "SDPhotoBrowser.h"
#import "SN_RegisterVIPViewController.h"
#import "SN_SetQuestionPwd_Controller.h"
#import "SN_InputQuestionPwd_Alert.h"
@interface SN_Question_Detail_Controller () <SN_Question_Detail_BottomViewDelegate,SDPhotoBrowserDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIImageView *questionImageView;
@property (nonatomic, strong) UIImageView *anserImageView;

@property (nonatomic, strong) SN_Question_Solve_Model *questionModel;

@property (nonatomic, strong) UIButton *showAnswerButton; //展开与收起答案

@property (nonatomic, strong) SN_Question_Detail_BottomView *bottomView;

@property (nonatomic, strong) UIButton *collectionButton;
@property (nonatomic, strong) UIButton *selectAllBtns;

@end

@implementation SN_Question_Detail_Controller {
    
    int tapType;  //1 代表点击的是问题图片 2 代表点击的是答案图片
    
    BOOL isSelectShowAllAnswers; //连续答题
    
    BOOL hasInputPwd; //已输入密码
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navBar.navigationItem.title = self.navTitle;
    
    
    //下一题手势
    UISwipeGestureRecognizer *swipLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipLeft)];
    swipLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipLeft];
    
    //上一题手势
    UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipRight)];
    swipRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipRight];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight - 50)];
    [self.view addSubview:_scrollView];
    
    _questionImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, VIEW_WIDTH, 140)];
    _questionImageView.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *questionTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(questionTapAction:)];
    [_questionImageView addGestureRecognizer:questionTap];
    
    _anserImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20 + 30, VIEW_WIDTH, 140)];
    _anserImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *answerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(questionTapAction:)];
    [_anserImageView addGestureRecognizer:answerTap];
    _anserImageView.hidden = YES;
    
    [_scrollView addSubview:_questionImageView];
    [_scrollView addSubview:_anserImageView];
    
    _questionModel = [self.listModel.questionlist objectAtIndex:self.selectIndex];
    
    
    _showAnswerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _showAnswerButton.frame = CGRectMake(VIEW_WIDTH - 80 - 15, VIEW_HEIGHT - SafeAreaTopHeight - 35 - 61, 80, 35);
    [_showAnswerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_showAnswerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_showAnswerButton setTitle:@"解题" forState:UIControlStateNormal];
    [_showAnswerButton setTitle:@"收起" forState:UIControlStateSelected];
    [_showAnswerButton setTitle:@"收起" forState:UIControlStateSelected | UIControlStateHighlighted];
    [_showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
    _showAnswerButton.layer.cornerRadius = 8;
    _showAnswerButton.titleLabel.font = [UIFont systemFontOfSize:17];
    //    [_showAnswerButton setImage:[UIImage imageNamed:@"answer_down"] forState:UIControlStateNormal];
//    [_showAnswerButton setImage:[UIImage imageNamed:@"answer_up"] forState:UIControlStateSelected];
//    [_showAnswerButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -_showAnswerButton.imageView.image.size.width + 5, 0, _showAnswerButton.imageView.image.size.width)];
//    _showAnswerButton.titleLabel.font = [UIFont systemFontOfSize:16];
//    [_showAnswerButton setImageEdgeInsets:UIEdgeInsetsMake(0, _showAnswerButton.titleLabel.width, 0, -_showAnswerButton.titleLabel.width - 10)];
    _showAnswerButton.hidden = YES;
    [_showAnswerButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
          [_scrollView addSubview:_showAnswerButton];
  
    
    [self requestQuestionImage];
    
    _bottomView = [[SN_Question_Detail_BottomView alloc] initWithFrame:CGRectMake(0, VIEW_HEIGHT - 50 - SafeAreaBottomHeight, VIEW_WIDTH, 50)];
    _bottomView.selectIndex = self.selectIndex;
    _bottomView.listModel = self.listModel;
    _bottomView.dataArr = self.dataArr;
    [_bottomView.catalogButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    _bottomView.delegate = self;
    [self.view addSubview:_bottomView];
    
    UIButton *setPwdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [setPwdBtn setTitle:@"设置解题密码" forState:UIControlStateNormal];
    [setPwdBtn setTitle:@"删除解题密码" forState:UIControlStateSelected];
    [setPwdBtn setTitle:@"删除解题密码" forState:UIControlStateSelected | UIControlStateHighlighted];
    
    [setPwdBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
    [setPwdBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateSelected];
    setPwdBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [setPwdBtn setFrame:CGRectMake(VIEW_WIDTH - 95 - 15, 10, 95, 30)];
    setPwdBtn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    [setPwdBtn addTarget:self action:@selector(setQuestionPwd:) forControlEvents:UIControlEventTouchUpInside];
    setPwdBtn.selected = [SYAccountTool ShareAccount].account.szbpwd.length >= 4;

    [_scrollView addSubview:setPwdBtn];
    

    
    [self setCollectionNav];
    
    _selectAllBtns = [UIButton buttonWithType:UIButtonTypeCustom];
    [_selectAllBtns setTitle:@"查看解题过程" forState:UIControlStateNormal];
    [_selectAllBtns setImage:[UIImage imageNamed:@"shaozhibao_jieti_nor"] forState:UIControlStateNormal];
    [_selectAllBtns setImage:[UIImage imageNamed:@"shaozhibao_jieti"] forState:UIControlStateSelected];
    [_selectAllBtns setTitle:@"查看解题过程" forState:UIControlStateSelected];
    [_selectAllBtns setFrame:CGRectMake(15, self.bottomView.frame.origin.y - 21 - 18, 150, 18)];
    [_selectAllBtns setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    _selectAllBtns.titleLabel.font = [UIFont systemFontOfSize:16];
    [_selectAllBtns setTitleEdgeInsets:UIEdgeInsetsMake(0, 12, 0, 0)];
    [_selectAllBtns addTarget:self action:@selector(selectAllAction:) forControlEvents:UIControlEventTouchUpInside];
    [_selectAllBtns setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateNormal];
    [_selectAllBtns setTitleColor:[UIColor colorWithHexString:@"#3b3b3b"] forState:UIControlStateSelected];
    _selectAllBtns.centerY = _showAnswerButton.centerY;
    [_scrollView addSubview:_selectAllBtns];
    
    isSelectShowAllAnswers = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeGround) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
//    self.navigationController.interactivePopGestureRecognizer.delegate = self;
}

- (void)setCollectionNav {
    
    if (!_collectionButton) {
        
        _collectionButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_collectionButton setFrame:CGRectMake(0, 0, 44, 44)];
        [_collectionButton setImageEdgeInsets:UIEdgeInsetsMake(0, 10, 0, -10)];
        [_collectionButton setImage:[UIImage imageNamed:@"unCollection"] forState:(UIControlStateNormal)];
        [_collectionButton setImage:[UIImage imageNamed:@"isCollection"] forState:UIControlStateSelected];
        [_collectionButton addTarget:self action:@selector(collectionAction:) forControlEvents:(UIControlEventTouchUpInside)];
            UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:_collectionButton];
        self.navBar.navigationItem.rightBarButtonItem = right;
    }
    
    _collectionButton.selected = _questionModel.iscollection;
    

}

- (void)requestQuestionImage {
    
    NSString *questionImgUrlStr = [NSString stringWithFormat:@"%@%@",PICPERFIXURL,_questionModel.problempic];
    NSURL *questionImgUrl = [NSURL URLWithString:questionImgUrlStr];
    
    [_questionImageView sd_setImageWithURL:questionImgUrl completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
       
        if (error) {
            
            [self showHint:error.localizedDescription];
        }else {
        
            CGFloat rate = image.size.height / image.size.width;
            
            CGFloat imgHeight = VIEW_WIDTH * rate;
            
            _questionImageView.height = imgHeight;
            
//            _showAnswerButton.y = CGRectGetMaxY(_questionImageView.frame) + 10;
            
            _showAnswerButton.hidden = NO;

            _showAnswerButton.y = CGRectGetMaxY(_questionImageView.frame) + 10;
            
            if (CGRectGetMaxY(_showAnswerButton.frame) > VIEW_HEIGHT - SafeAreaTopHeight - 50 - SafeAreaBottomHeight) {
                
                _scrollView.contentSize = CGSizeMake(VIEW_WIDTH, CGRectGetMaxY(_showAnswerButton.frame) + 10);
                
            }else {
                
                _showAnswerButton.y = VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight - 61 - 35;
                _scrollView.contentSize = CGSizeMake(VIEW_WIDTH, CGRectGetMaxY(_showAnswerButton.frame));
            }
            
            _selectAllBtns.centerY = _showAnswerButton.centerY;
            
//            if (CGRectGetMaxY(_questionImageView.frame) > VIEW_HEIGHT - 64 - 50) {
//            
//                _scrollView.contentSize = CGSizeMake(VIEW_WIDTH, CGRectGetMaxY(_questionImageView.frame));
//                
//                _showAnswerButton.y = CGRectGetMaxY(_questionImageView.frame) + 10;
//            }else {
//                
//                _showAnswerButton.y = VIEW_HEIGHT - 64 - 35 - 61;
//            }
        }
        
    }];
    
    
}

- (void)requestAnswerImage {
    
    NSString *answerImgUrlStr = [NSString stringWithFormat:@"%@%@",PICPERFIXURL,_questionModel.thinkingpic];
    NSURL *answerImgUrl = [NSURL URLWithString:answerImgUrlStr];

    [_anserImageView sd_setImageWithURL:answerImgUrl completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
        if (error) {
            
            [self showHint:error.localizedDescription];
        }else {
            
            CGFloat rate = image.size.height / image.size.width;
            
            CGFloat imgHeight = VIEW_WIDTH * rate;
            
            _anserImageView.height = imgHeight;
            _anserImageView.y = CGRectGetMaxY(self.questionImageView.frame) + 30;
            
            
            _showAnswerButton.y = CGRectGetMaxY(_anserImageView.frame) + 10;
            
            
            if (CGRectGetMaxY(_showAnswerButton.frame) > VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight - 50) {
                
                _scrollView.contentSize = CGSizeMake(VIEW_WIDTH, CGRectGetMaxY(_showAnswerButton.frame) + 10);
                
            }else {
                
                _showAnswerButton.y = VIEW_HEIGHT - SafeAreaTopHeight - SafeAreaBottomHeight - 61 - 35;
                _scrollView.contentSize = CGSizeMake(VIEW_WIDTH, CGRectGetMaxY(_showAnswerButton.frame));
            }

            _selectAllBtns.centerY = _showAnswerButton.centerY;
            
        }
        
    }];
}

#pragma mark -SN_Question_Detail_BottomViewDelegate
- (void)questionBtnClick:(SN_Question_Solve_Model*)selectModel {
    
    //隐藏答案
    _showAnswerButton.selected = NO;
    _anserImageView.hidden = !isSelectShowAllAnswers;
    
    _questionModel = selectModel;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:@"api_szb_readquestion" forKey:@"method"];
    

    [dict setObject:_questionModel.problemid forKey:@"problemid"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSArray *arr = [jsonObject objectForKey:@"re_data"];
        NSDictionary *dict = [arr objectAtIndex:0];
        
        
        _questionModel = [SN_Question_Solve_Model mj_objectWithKeyValues:dict];
        
        
        NSString *isvip = [NSString stringWithFormat:@"%@",[dict objectForKey:@"isvip"]];
        
        NSDictionary *imformation = [SyUserInfomation acquirePersonalImformationFromMemoryWithPersonID:USERID];
        NSMutableDictionary *informationM = [NSMutableDictionary dictionaryWithDictionary:imformation];
        
        [informationM setObject:isvip forKey:@"isvip"];
        
        [SyUserInfomation personalImformationSaveWtih:informationM];

        
        
        [self requestQuestionImage];
        
        if (isSelectShowAllAnswers) {
            
            [self requestAnswerImage];
        }
        
        
        
        [self setCollectionNav];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];

}

#pragma mark -查看解题思路
- (void)btnClick:(UIButton *)sender {
    
    if (sender.isSelected) { //收起操作

        sender.selected = NO;
        self.anserImageView.hidden = YES;
        [self requestQuestionImage];
        return;
    }
    
    //不是vip的  先充值再说
    if (![[SYAccountTool ShareAccount].account.isvip isEqualToString:@"1"]) {  //1.是vip
        
        SN_RegisterVIPViewController *vc = [[SN_RegisterVIPViewController alloc] init];
        vc.isRootViewController = YES;
        SYNavigationController *nav = [[SYNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
        
        return;
    }
    
    if (![SYAccountTool ShareAccount].hasInputpwd) { //没有输入密码
        
        //查看答案操作
        if ([SYAccountTool ShareAccount].account.szbpwd && [SYAccountTool ShareAccount].account.szbpwd.length >= 4) { //已经设置结题密码
            
            SN_InputQuestionPwd_Alert *alert = [[SN_InputQuestionPwd_Alert alloc] initWithSuperView:self.view];
            alert.confirmBlock = ^(){
                
                [SYAccountTool ShareAccount].hasInputpwd = YES;
                
                if (sender.selected == NO) {
                    
                    [self requestAnswerImage];
                    
                }
                else {
                    [self requestQuestionImage];
                }
                
                sender.selected = !sender.selected;
                
                self.anserImageView.hidden = !sender.selected;
                
            };
            return;
        }

    }
    
    if (sender.selected == NO) {
        
        [self requestAnswerImage];
        
    }
    else {
        [self requestQuestionImage];
    }
    
    sender.selected = !sender.selected;
    
//    self.questionImageView.hidden = sender.selected;
    self.anserImageView.hidden = !sender.selected;
    
//    if (self.showAnswerButton.isSelected) {
//        [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#eab1ae"]];
//    }else {
//        [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
//    }
}

//返回目录
- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
}

//收藏
- (void)collectionAction:(UIButton *)sender {
    
    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
    
    [dicM setObject:@"api_szb_setproblemcollection" forKey:@"Method"];
    
    NSString *state = sender.isSelected ? @"0" : @"1";
    [dicM setObject:_questionModel.problemid forKey:@"problemid"];
    [dicM setObject:state forKey:@"state"];
    [dicM setObject:USERID forKey:@"userid"];
    [Utils getWithparameters:dicM showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        sender.selected = !sender.selected;
        
        if (self.collectionBlock) { //上个界面刷新数据
            
            self.collectionBlock();
        }
        [self showHint:sender.isSelected == YES?@"收藏成功":@"取消成功"];
        
    } failure:^(NSInteger recode, NSError *error) {
       
        [self showHint:error.localizedDescription];
    }];
}

#pragma mark -图片点击
- (void)questionTapAction:(UITapGestureRecognizer *)gezr {
    
    UIImageView *imgV = (UIImageView *)gezr.view;
    
    tapType = imgV == _questionImageView ? 1 : 2;
    
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
    
    browser.imageCount = 2;
    browser.currentImageIndex = tapType - 1;
    browser.delegate = self;
    browser.sourceImagesContainerView = _scrollView;
//    browser.noAnimation = YES;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index flag:(NSString *)flagStr{
    
    if (index == 0) {
        
        return _questionImageView.image;
    }else {
        return _anserImageView.image;
    }
}


- (void)swipLeft {
    
    NSArray *btnArr = _bottomView.scrollView.subviews;
    
    int selectIndex = 0;
    UIButton *selectedBtn;
    for (int i = 0; i < btnArr.count - 1; i++) {
        
        UIButton *btn = [btnArr objectAtIndex:i];
        
        if (btn.selected == YES) {
            
            selectIndex = i;
            
            if (i + 1 < btnArr.count - 1) {
                
                selectedBtn = [btnArr objectAtIndex:(i + 1)];
                [selectedBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
                
                if (_bottomView.scrollView.contentOffset.x + _bottomView.scrollView.frame.size.width < _bottomView.scrollView.contentSize.width) {
                    
                    [UIView animateWithDuration:0.3 animations:^{
                        
                        _bottomView.scrollView.contentOffset = CGPointMake(_bottomView.scrollView.contentOffset.x + _bottomView.scrollView.frame.size.width / 4, 0);
                        
                    } completion:nil];
                }
                
                return;
            }
        }
    }
    
}

- (void)swipRight {
    
    NSArray *btnArr = _bottomView.scrollView.subviews;
    
    int selectIndex = 0;
    UIButton *selectedBtn;
    for (int i = 0; i < btnArr.count - 1; i++) {
        
        UIButton *btn = [btnArr objectAtIndex:i];
        
        if (btn.selected == YES) {
            
            selectIndex = i;
            
            if (i - 1 >= 0 ) {
                
                selectedBtn = [btnArr objectAtIndex:(i - 1)];
                [selectedBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
                
                if (_bottomView.scrollView.contentOffset.x > 0) {
                    
                    [UIView animateWithDuration:0.3 animations:^{
                        
                        _bottomView.scrollView.contentOffset = CGPointMake(_bottomView.scrollView.contentOffset.x - _bottomView.scrollView.frame.size.width / 4, 0);
                        
                    } completion:nil];
                    
                }
                
                return;
            }
        }
    }
}

#pragma mark -设置结题密码
- (void)setQuestionPwd: (UIButton *)sender {
    

    SN_SetQuestionPwd_Controller *vc = [[SN_SetQuestionPwd_Controller alloc] init];
    
    vc.type = sender.selected ? 2 : 1;
    
    vc.block = ^(int type) {
        
        sender.selected = type;
        
        if (type == 1) { //设置密码
            
            _selectAllBtns.selected = false;
            isSelectShowAllAnswers = false;
            _anserImageView.hidden = YES;
            _showAnswerButton.userInteractionEnabled = YES;
            _showAnswerButton.selected = NO;
            [_showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
            [SYAccountTool ShareAccount].hasInputpwd = false;
            [self requestQuestionImage];
        }
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}

//查看所有解题过程
- (void)selectAllAction:(UIButton *)sender {
    
    //不是vip的  先充值再说
    if (![[SYAccountTool ShareAccount].account.isvip isEqualToString:@"1"]) {  //1.是vip
        
        SN_RegisterVIPViewController *vc = [[SN_RegisterVIPViewController alloc] init];
        vc.isRootViewController = YES;
        SYNavigationController *nav = [[SYNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
        
        return;
    }
    
    
    if ([SYAccountTool ShareAccount].account.szbpwd && [SYAccountTool ShareAccount].account.szbpwd.length >= 4 && ![SYAccountTool ShareAccount].hasInputpwd) { //设置了密码 并且还没有输入过
    
        SN_InputQuestionPwd_Alert *alert = [[SN_InputQuestionPwd_Alert alloc] initWithSuperView:self.view];
        

        alert.confirmBlock = ^(){
            
            sender.selected = !sender.selected;
            [SYAccountTool ShareAccount].hasInputpwd = YES;
            
            isSelectShowAllAnswers = sender.isSelected;
            
            if (sender.isSelected) {
                
                [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#eab1ae"]];
                self.showAnswerButton.userInteractionEnabled = NO;
                
                self.anserImageView.hidden = NO;
                [self requestAnswerImage];
                
            }else {
                [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
                self.showAnswerButton.userInteractionEnabled = YES;
                self.anserImageView.hidden = YES;
                self.showAnswerButton.selected = NO;
                [self requestQuestionImage];
            }

        };
        
    }else { //没有设置密码  或已输入过
    
        sender.selected = !sender.selected;
        
        isSelectShowAllAnswers = sender.isSelected;
        
        if (sender.isSelected) {
            
            [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#eab1ae"]];
            self.showAnswerButton.userInteractionEnabled = NO;
            
            self.anserImageView.hidden = NO;
            [self requestAnswerImage];
            
        }else {
            [self.showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
            self.showAnswerButton.userInteractionEnabled = YES;
            self.anserImageView.hidden = YES;
            self.showAnswerButton.selected = NO;
            [self requestQuestionImage];
        }

    }
    
}

#pragma mark -UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    
    return NO;
}

#pragma mark -NSNotification
- (void)appEnterBackground {
    
    
    if ([SYAccountTool ShareAccount].account.szbpwd.length >= 4) {
        
        _anserImageView.hidden = YES;
        [SYAccountTool ShareAccount].hasInputpwd = false;
        _showAnswerButton.selected = NO;
        _selectAllBtns.selected = NO;
        isSelectShowAllAnswers = NO;
        [_showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
        _showAnswerButton.userInteractionEnabled = YES;
        [self requestQuestionImage];
    }
    
}

- (void)appEnterForeGround {
    
    /*
    return;
    
    if (isSelectShowAllAnswers) {
        
        
        if ([SYAccountTool ShareAccount].account.szbpwd.length >= 4) { //设置了密码 再次校验
            
            SN_InputQuestionPwd_Alert *alert = [[SN_InputQuestionPwd_Alert alloc] initWithSuperView:self.view];
            alert.confirmBlock = ^(){
                
                _anserImageView.hidden = NO;
            };
            
            alert.cancelBlock = ^() { //取消
              
                isSelectShowAllAnswers = NO;
                
                [SYAccountTool ShareAccount].hasInputpwd = NO;
                
                _selectAllBtns.selected = NO;
                
                _showAnswerButton.selected = NO;
                
                [_showAnswerButton setBackgroundColor:[UIColor colorWithHexString:@"#fb524a"]];
                _showAnswerButton.userInteractionEnabled = YES;
            };
        }

        return;
    }
    
    if (_showAnswerButton.isSelected) { //处于查看解题思路状态
        
        if ([SYAccountTool ShareAccount].account.szbpwd.length >= 4) { //设置了密码 再次校验
            
            SN_InputQuestionPwd_Alert *alert = [[SN_InputQuestionPwd_Alert alloc] initWithSuperView:self.view];
            alert.confirmBlock = ^(){
            
                _anserImageView.hidden = NO;
            };
            
            alert.cancelBlock = ^() {
            
                _showAnswerButton.selected = NO;
                [SYAccountTool ShareAccount].hasInputpwd = NO;
            };
        }
        
    }
     */
    
}

@end
